package com.qihu.customnotification01;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 先创建builderCompat 设置ticket等
 * 在设置2个remoteViews,分别是正常显示和伸展后的显示
 */
public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Context context = this;
    private long[] pattern;

    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;
    @BindView(R.id.btn3)
    Button btn3;
    @BindView(R.id.btn4)
    Button btn4;
    @BindView(R.id.btn5)
    Button btn5;
    @BindView(R.id.btn6)
    Button btn6;
    @BindView(R.id.btn7)
    Button btn7;
    @BindView(R.id.btn8)
    Button btn8;
    @BindView(R.id.btn9)
    Button btn9;
    @BindView(R.id.btn10)
    Button btn10;
    @BindView(R.id.btn11)
    Button btn11;
    @BindView(R.id.btn12)
    Button btn12;
    @BindView(R.id.btn13)
    Button btn13;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        pattern = new long[]{0, 2000, 50000, 2000, 5000, 2000};//时间间隔最长50s

        Log.d(TAG, "MainActivity  onCreate()");
    }

    @OnClick({R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.btn5, R.id.btn6, R.id.btn7, R.id.btn8, R.id.btn9, R.id.btn10, R.id.btn11, R.id.btn12, R.id.btn13})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                showNotification1();
                break;
            case R.id.btn2:
                showNotification2();
                break;
            case R.id.btn3:
                showNotification3();
                break;
            case R.id.btn4:
                showNotification4();
                break;
            case R.id.btn5:
                showNotification5();
                break;
            case R.id.btn6:
                showNotification6();
                break;
            case R.id.btn7:
                showNotification7();
                break;
            case R.id.btn8:
                showNotification8();
                break;
            case R.id.btn9:
                showNotification9();
                break;
            case R.id.btn10:
                showNotification10();
                break;
            case R.id.btn11:
                showNotification11();
                break;
            case R.id.btn12:
                showNotification12();
                break;
            case R.id.btn13:
                showNotification13();
                break;
        }
    }


    // vibrate只可以设置一次，用setVibrate()就不可以在用Notification.DEFAULT_VIBRATE,而Notification.DEFAULT_ALL包含DEFAULT_VIBRATE，导致无效setVibrate(pattern)无效
    private void showNotification1() {
        Notification.Builder builder = new Notification.Builder(context);

        builder.setContentTitle("正常通知")
                .setContentText("message" + getResources().getString(R.string.notification_normal))
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setTicker("Ticker")
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)//和setVibrate(pattern)可以同用
//                .setDefaults(Notification.DEFAULT_ALL)//和setVibrate(pattern)不可以同用
//                .setVibrate(pattern)
                .setPriority(Notification.PRIORITY_DEFAULT);
        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_INSISTENT;//会一直有声音+震动，不可以与Notification.DEFAULT_ALL共用
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(1, notification);
    }


    private void showNotification2() {
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_normal);
        contentView.setTextViewText(R.id.tv_normal, getResources().getString(R.string.notification_normal));
        contentView.setTextColor(R.id.tv_normal, Color.RED);

        RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.notification_enpanded);
        bigContentView.setTextViewText(R.id.tv_expanded, getResources().getString(R.string.notigication_expanded));
        bigContentView.setTextColor(R.id.tv_expanded, Color.RED);

        RemoteViews headerContentView = new RemoteViews(getPackageName(), R.layout.notification__header_up);
        bigContentView.setTextViewText(R.id.tv_header, "Header");

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle("自定义通知布局RemoteViews一")//占用一行
                .setContentText("message")//占用一行
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setTicker("Ticker")
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContent(contentView)//效果和setCustomContentView一样，同样是占用4行，
                .setCustomContentView(contentView)//占用4行
                .setCustomBigContentView(bigContentView)//最大
        //                .setCustomHeadsUpContentView(headerContentView)//无效
        ;
        Notification notification = builder.build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(2, notification);
    }

    //当设置自定义通知时，setContentTitle()/setContentText()/setTicker()都无效
    private void showNotification3() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle("自定义通知布局RemoteViews二")
                .setContentText("message")
                .setDefaults(Notification.DEFAULT_ALL)
                .setTicker("ticker")
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher_round);
        Notification notification = builder.build();

        //自定义contentView
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_normal);
        contentView.setTextViewText(R.id.tv_normal, getResources().getString(R.string.notification_normal));
        notification.contentView = contentView;

        //自定义bigContentView
        if (Build.VERSION.SDK_INT >= 16) {
            RemoteViews bigContentView = new RemoteViews(getPackageName(), R.layout.notification_enpanded);
            bigContentView.setTextViewText(R.id.tv_expanded, getResources().getString(R.string.notigication_expanded));
            notification.bigContentView = bigContentView;
        }

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(3, notification);
    }


    private void showNotification4() {
        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        inboxStyle.setBigContentTitle("BigContentTitle:");
        for (int i = 0; i < 6; i++) {
            inboxStyle.addLine("item  " + i);
        }

        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle("Inbox扩展布局(相当于在通知中加入了ListView)")//扩展后就不显示了
                .setContentText("text")//扩展后就不显示了
                .setTicker(SystemClock.currentThreadTimeMillis() + "")
                .setAutoCancel(true)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setDefaults(Notification.DEFAULT_ALL)
                .setNumber(5)//扩展后就不显示了
                .setStyle(inboxStyle)
                .build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(5, notification);
    }

    /**
     * TaskStackBuilder形式
     * 点击通知进入NewActivity会销毁当前的MainActivity
     */
    private void showNotification5() {
        Intent intent = new Intent(context, NewActivity.class);
        TaskStackBuilder builder = TaskStackBuilder.create(context)
                .addParentStack(NewActivity.class)
                .addNextIntent(intent);
        PendingIntent pendingIntent = builder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        /**
         FLAG_IMMUTABLE   FLAG_ONE_SHOT才可以返回 MainActivity
         */


        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle("TaskStackBuilder（启动 Activity 时保留导航）")
                .setContentText("text")
                .setTicker(SystemClock.currentThreadTimeMillis() + "")
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setDefaults(Notification.DEFAULT_ALL)
                .setNumber(5)
                .build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(4, notification);
        Log.d(TAG, SystemClock.currentThreadTimeMillis() + "");
    }


    private void showNotification6() {
        Intent intent = new Intent(context, NewActivity2.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new NotificationCompat.Builder(context)
                .setContentTitle("设置特殊 Activity PendingIntent")
                .setContentText("text")
                .setTicker(SystemClock.currentThreadTimeMillis() + "")
                .setAutoCancel(true)
                .setContentIntent(pendingIntent)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setDefaults(Notification.DEFAULT_ALL)
                .setNumber(5)
                .build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(6, notification);
        Log.d(TAG, SystemClock.currentThreadTimeMillis() + "");
    }


    private void showNotification7() {
        final Notification.Builder builder = new Notification.Builder(context);

        builder.setContentTitle("在通知中显示进度一：显示持续 Activity指示器")
                .setContentText("message" + getResources().getString(R.string.notification_normal))
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setTicker("Ticker")
                .setAutoCancel(true)
                .setProgress(0, 0, true)
                .setDefaults(Notification.DEFAULT_ALL);
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 30; i += 5) {
                    builder.setProgress(0, 0, true);
                    manager.notify(1, builder.build());
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                builder.setContentText("下载完成").setProgress(0, 0, false);//移除通知
                manager.notify(7, builder.build());
            }
        }).start();
    }


    private void showNotification8() {
        final Notification.Builder builder = new Notification.Builder(context);

        builder.setContentTitle("在通知中显示进度二：显示持续时间固定的进度指示器")
                .setContentText("message" + getResources().getString(R.string.notification_normal))
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setTicker("Ticker")
                .setAutoCancel(true)
                .setProgress(100, 0, false)
                .setDefaults(Notification.DEFAULT_ALL);
        final NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i <= 10; i += 5) {
                    builder.setProgress(100, i, false);
                    manager.notify(8, builder.build());
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                builder.setContentText("下载完成").setProgress(0, 0, false);//移除通知
                manager.notify(8, builder.build());
            }
        }).start();
    }

    private void showNotification9() {

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        builder.setContentTitle("锁定屏幕通知")
                .setContentText("message锁定屏幕通知")
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setTicker("Ticker")
                .setAutoCancel(true)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .setVisibility(Notification.VISIBILITY_PUBLIC)//API>=21  锁定屏幕通知
                .setDefaults(Notification.DEFAULT_ALL);//和setVibrate(pattern)可以同用
        Notification notification = builder.build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(9, notification);
    }

    private void showNotification10() {
        Notification.Action start = new Notification.Action(R.mipmap.ic_launcher_round, "start", null);
        Notification notification = new Notification.Builder(context)
                .setVisibility(Notification.VISIBILITY_PUBLIC)
                // Add media control buttons that invoke intents in your media service
                .addAction(R.mipmap.ic_launcher_round, "Previous", null) // #0
                .addAction(R.mipmap.ic_launcher_round, "Pause", null)  // #1
                .addAction(R.mipmap.ic_launcher, "Next", null)     // #2
                .addAction(start)
                .setStyle(new Notification.MediaStyle()// Apply the media style template
                                .setShowActionsInCompactView(1)
//                        .setMediaSession(mMediaSession.getSessionToken()
                )
                .setContentTitle("在锁定屏幕上控制媒体播放(还没搞好)")
                .setContentText("My Awesome Band")
                .build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(10, notification);
    }


    private void showNotification11() {
        Intent intent = new Intent(context, NewActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentTitle("浮动通知")
                .setContentText("message" + getResources().getString(R.string.notification_normal))
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setTicker("Ticker")
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setFullScreenIntent(pendingIntent, false)
                .setPriority(Notification.PRIORITY_DEFAULT);
        Notification notification = builder.build();

        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(11, notification);
    }

    private void showNotification12() {
        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentTitle("浮动通知")
                .setContentText("message" + getResources().getString(R.string.notification_normal))
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setTicker("Ticker")
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS)
                .setPriority(Notification.PRIORITY_HIGH);
        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_INSISTENT;
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(12, notification);
    }


    private void showNotification13() {
        Notification.Builder builder = new Notification.Builder(context);

        builder.setContentTitle("一直有声音+震动+呼吸灯")
                .setContentText("message" + getResources().getString(R.string.notification_normal))
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setTicker("Ticker")
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)
                .setPriority(Notification.PRIORITY_DEFAULT);
        Notification notification = builder.build();
        notification.flags |= Notification.FLAG_INSISTENT;//第一次：声音+震动，之后只有声音，不可以与Notification.DEFAULT_ALL共用
        NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        manager.notify(13, notification);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "MainActivity  onDestroy()");
    }
}
